<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="flexrow">
            <div class="layui-panel list-left-container" style="width: 200px;">
                <ul id="datatree" class="ztree"></ul>
            </div>
            <div class="layui-panel list-right-container">
                <div class="list-search-container">
                    <button class="layui-btn layui-btn-sm" onclick="HandleAdd()"><i class="layui-icon layui-icon-add-1 layui-font-12"></i>新增数据字典</button>
                    <button class="layui-btn layui-btn-sm" onclick="ExportData()"><i class="layui-icon layui-icon-download-circle layui-font-12"></i>导出数据</button>
                    <button class="layui-btn layui-btn-sm" onclick="ImportData()"><i class="layui-icon layui-icon-upload-circle layui-font-12"></i>导入数据</button>
                    <div style="display: none">
                        <input type="file" name="file" id="file" onchange="uploadXml()" accept=".xml" />
                    </div>
                    <div class="list-search-item">
                        <span style="width: 140px">数据字典名称：</span>
                        <input id="code_name" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindData()"><i class="layui-icon layui-icon-search layui-font-12"></i>搜索</button>
                </div>
                <table class="layui-table" lay-data="{id: 'datagrid'}" lay-filter="datagrid">
                    <thead>
                        <tr>
                            <th lay-data="{type:'checkbox'}"></th>
                            <th lay-data="{field:'code_name',width:200}">数据字典名称</th>
                            <th lay-data="{field:'remark'}">说明</th>
                            <th lay-data="{field:'sort_num',width:80,align:'center'}">排序</th>
                            <th lay-data="{fixed: 'right', width:250, align:'center', toolbar: '#op'}">操作</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</body>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleCodeItem"><i class="layui-icon layui-icon-template-1 layui-font-12"></i>维护</button>
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i class="layui-icon layui-icon-edit layui-font-12"></i>编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="HandleDelete"><i class="layui-icon layui-icon-delete layui-font-12"></i>删除</button>
</script>
<script>
    var group_id = "";

    $(function() {
        BindData();
    })

    function BindData() {
        BindTableData();
        BindTreeData();
    }

    function BindTableData() {
        var param = {
            code_name: $("#code_name").val(),
            group_id: group_id
        }
        BindTable("datagrid", "system/setting/codemain/list", param)
    }

    function TreeNodeClick(event, treeId, treeNode, clickFlag) {
        group_id = treeNode.id;
        BindTableData();
    }


    function BindTreeData() {
        BindTree("datatree", "system/setting/group/tree", { groupTag: "DataDic" }, TreeNodeClick);
    }

    function HandleAdd() {
        OpenDialog('新增数据字典', 'system/datadic/codemian_add.html?group_id=' + group_id, 800, 600, function() {
            BindData();
        })
    }

    function HandleEdit(RowData) {
        OpenDialog('修改数据字典', 'system/datadic/codemain_edit.html?row_id=' + RowData.row_id, 800, 600, function() {
            BindData();
        })
    }

    function HandleCodeItem(RowData){
        OpenDialog('字典维护', 'system/datadic/codeitem_list.html?code_id=' + RowData.row_id, 800, 600)
    }

    function HandleDelete(RowData) {
        OpenConfirm('确认删除？', function() {
            var param = {
                row_id: RowData.row_id
            }
            HttpPost(JSON.stringify(param), 'system/setting/codemain/delete', function(result) {
                if (result.code == 0) {
                    OpenSuccessMessage("删除成功")
                    BindData();
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }

    function ExportData() {
        var checkData = GetCheckData();
        if (checkData.length > 0) {
            StartLoading("导出中");
            var rowIDList = [];
            checkData.forEach(element => {
                rowIDList.push(element.row_id)
            });
            HttpPost(JSON.stringify(rowIDList), 'system/setting/codemain/exportData', function(result) {
                CloseLoading();
                if (result.code == 0) {
                    window.top.open(GetApiPath() + "/system/common/attach/readExportXmlFile?fileid=" + result.data.fileid + "&token=" + GetTokenID())
                } else {
                    OpenFail(result.msg);
                }
            })
        } else {
            OpenWarning("未选择数据")
        }
    }

    function ImportData() {
        document.getElementById("file").click();
    }

    function uploadXml() {
        StartLoading("导入中");
        var formData = new FormData();
        formData.append('file', document.getElementById('file').files[0]);
        HttpForm(formData, 'system/setting/codemain/importData', function(result, status) {
            CloseLoading();
            if (result.code == 0) {
                OpenSuccess('导入成功', function() {
                    BindData();
                })
            } else {
                OpenFail(result.msg);
            }
            document.getElementById("file").value = "";
        })
    }
</script>

</html>